package com.ruoyi.post.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.experiment.domain.Experiment;
import com.ruoyi.post.model.BriefPost;
import com.ruoyi.post.model.FinishInfo;
import com.ruoyi.post.model.PExperiment;
import com.ruoyi.post.model.Tops;
import com.ruoyi.post.service.IJobInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/post/post")
public class PostController extends BaseController {

    @Autowired
    IJobInformationService jobService;

    @GetMapping("list")
    public TableDataInfo getList(){
        startPage();
        List<BriefPost> list = jobService.getList();
        return getDataTable(list);
    }

    @GetMapping("type/list")
    public AjaxResult getListByType(Integer type){
        List<BriefPost> list = jobService.selectPostByType(type);
        return AjaxResult.success(list);
    }
    @GetMapping("manager/set")
    public AjaxResult setJob(Long experimentId,Integer jobId){
        jobService.setJob(experimentId,jobId);
        return AjaxResult.success();
    }

    /**
     * 获取所有分配给内部实验管理员的实验
     * @return
     */
    @GetMapping("manager/list")
    public TableDataInfo getManagerList(){
        startPage();
        List<PExperiment> pExperiments = jobService.selectManagerExperiment();
        return getDataTable(pExperiments);
    }

    @GetMapping("type")
    public AjaxResult getExperimentTypes(Integer jobId){
        return success(jobService.getExperimentTypes(jobId));
    }

    @GetMapping("experiment")
    public TableDataInfo getExperiments(Experiment experiment){
        startPage();
        if(experiment.getTypeId()==null)
            return getDataTable(new ArrayList<>());
        List<PExperiment> experiments = jobService.getExperiments(experiment);
        return getDataTable(experiments);
    }

    /**
     * 完成内部实验
     * @param finishInfo
     * @return
     */
    @PostMapping("finish")
    public AjaxResult finish(@RequestBody FinishInfo finishInfo){
        Boolean b = jobService.finishExperiment(finishInfo);
        if (b){
            return success();
        }else {
            return new AjaxResult(208,"前置流程未完成",null);
        }
    }
    @PostMapping("top")
    public AjaxResult top(@RequestBody Tops tops){
        jobService.top(tops);
        return success();
    }
    @GetMapping("canceltop")
    public AjaxResult cancelTop(Long[] ids){
        jobService.cancelTop(Arrays.asList(ids));
        return success();
    }
}
